<script setup lang="ts">
import { createSubject } from '@/api/subject';
import type { Subject } from '@/api/subject/type';
import YHeader from '@/components/YHeader.vue';
import { showToast, UploaderFileListItem } from 'vant';
import { ref } from 'vue';
import Required from '@/components/icons/Required.vue';
import { Result } from '@/utils/request';
import { router } from '@/router';
const subject = ref({
    name: ''
} as Subject);
const file = ref({} as {
    url: string,
    avatar: File
});
const afterRead = (uploader: UploaderFileListItem | UploaderFileListItem[]) => {
    if (!Array.isArray(uploader) && uploader.objectUrl != undefined && uploader.file != undefined) {
        file.value.url = uploader.objectUrl;
        file.value.avatar = uploader.file;
    }
}
const onSubmit = () => {
    createSubject(subject.value, file.value.avatar).then((res) => {
        const {msg} = res as Result;
        showToast(msg);
        router.back();
    })
}
const removeTag = () => {

}
</script>

<template>
    <div class="container">
        <YHeader :name="'创建题目集'"></YHeader>
        <div class="content">
            <div class="center uploader">
                <van-uploader :after-read="afterRead" :reupload="true" preview-size="100">
                    <van-image
                        width="100"
                        height="100"
                        :src="file.url"
                    ></van-image>
                </van-uploader>
            </div>
            <div class="center form">
                <div class="row">
                    <div class="name">
                        <p>题目集名称</p>
                        <Required></Required>
                    </div>
                    <van-field class="input" v-model="subject.name" placeholder="名称简单明了~" />
                </div>
                <div class="row">
                    <div class="name">
                        <p>题目集标签</p>
                        <Required></Required>
                    </div>
                    <div class="tag">
                        <van-tag round type="primary" closeable @close="removeTag">mysql</van-tag>
                    </div>
                </div>
                <div class="tag">
                    <div></div>
                </div>
                <div style="margin: 16px; width: 80%;">
                    <van-button :disabled="subject.name.length === 0" round block type="primary" native-type="submit" @click="onSubmit">
                    立即创建
                    </van-button>
                </div>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.center {
    justify-content: center;
    align-items: center;
}
.content {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .uploader {
        padding: 3%;
        margin-top: 3%;
        width: 30%;
        border-radius: 15px;
    }
    .form {
        width: 94%;
        margin: 3%;
        justify-content: center;
        align-items: center;
        display: flex;
        flex-direction: column;
        .row {
            display: flex;
            width: 100%;
            .name {
                width: 40%;
                display: flex;
                align-items: center;
            }
            .input {
                border-radius: 15px;
            }
            .tag {
                display: flex;
                align-items: center;
            }
        }
    }
}
</style>